<template>
  <div id="GlobalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img src="../assets/logo.png" class="logo" alt="logo" />
            <div class="title">云图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="160px">
        <div v-if="loginUserStore.loginUser.id">
          <a-dropdown>
            <a-space>
              <a-avatar :src="loginUserStore.loginUser.userAvatar"
              >{{ loginUserStore.loginUser.userName.slice(0, 4) }}
              </a-avatar>
              {{ loginUserStore.loginUser.userName }}
              <DownOutlined />
            </a-space>
            <template #overlay>
              <a-menu>
                <a-menu-item>
                  <router-link to="/my_space">
                    <UserOutlined />
                    我的空间
                  </router-link>
                </a-menu-item>
                <a-menu-item @click="() => router.push('/user/update')">
                  <LogoutOutlined/>
                  个人信息
                </a-menu-item>
                <a-menu-item @click="doLogout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
        <div v-else class="login-btn">
          <a-button type="primary" @click="showAuthModal">登录</a-button>
          <a-modal class="auth-modal" v-model:open="authModalVisible" :footer="null">
            <AuthForm  />
          </a-modal>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import {computed, h, ref, onMounted, onUnmounted} from 'vue'
import {
  MailOutlined,
  AppstoreOutlined,
  SettingOutlined,
  HomeOutlined,
  DownOutlined,
  LogoutOutlined,
  UserOutlined,
} from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'
import PictureUpload from '@/components/PictureUpload.vue'
import UserLoginPage from '@/pages/user/UserLoginPage.vue'
import UserRegisterPage from "@/pages/user/UserRegisterPage.vue";
import {eventBus} from "@/eventBus.ts";
import AuthForm from "@/components/AuthForm.vue";

const current = ref<string[]>([])

const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '首页',
    title: '首页',
  },
  {
    key: '/admin/userManage',
    icon: () => h(AppstoreOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManage',
    icon: () => h(AppstoreOutlined),
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/spaceManage',
    icon: () => h(AppstoreOutlined),
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: '/add_picture',
    label: '图片创建',
    title: '图片创建',
  },
]

const authModalVisible = ref<boolean>(false)

const showAuthModal = () => {
  authModalVisible.value = true
}

// 根据权限过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    // 管理员才能看到 /admin 开头的菜单
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

const items = computed(() => filterMenus(originItems))

const loginUserStore = useLoginUserStore()

const router = useRouter()

// 路由点击跳转事件
const doMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}

const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    //router.push('/user/login')
  } else {
    message.error('退出登录失败')
  }
}

// 监听路由变化，更新高亮菜单
router.afterEach((to, from, next) => {
  current.value = [to.path]
})


onMounted(() => {
    eventBus.on('showLogin', showAuthModal)
})

onUnmounted(() => {
    eventBus.off('showLogin', showAuthModal)
})

</script>
<style scoped>
#GlobalHeader .title-bar {
  display: flex;
  align-items: center;
}

.title {
  font-size: 18px;
  color: black;
  margin-left: 10px;
}

.logo {
  height: 40px;
}

</style>

<style>
/* 全局样式 */
.auth-modal .ant-modal-content {
  padding: 16px 0 0 0 !important;
}
</style>
